package com.ch.base.factory;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

public class ConnectionFactory {
	private final Logger log = Logger.getLogger(this.getClass());

//	private static ConnectionFactory instance = null;
//	private static ConnectionFactory instance2 = null;

	private Connection conn = null;
	private String db = null;
	private String driver = null;
	private String url = null;
	private String user = null;
	private String userPwd = null;

	public ConnectionFactory() throws Exception {
		HttpServletRequest request;
		request = ServletActionContext.getRequest();

		String hostMilieu = request.getSession().getServletContext().getInitParameter("hostMilieu");
		if (hostMilieu.equalsIgnoreCase("server")) {
			initDatabaseSource(request.getContextPath().replace("/", ""));
		} else if ((hostMilieu == null) || (hostMilieu.equalsIgnoreCase("test"))) {
			initDatabaseSource("Test_" + request.getContextPath().replace("/", ""));
		} else if (hostMilieu.equalsIgnoreCase("com")) {
			initDatabaseSource("Com_" + request.getContextPath().replace("/", ""));
		} else if (hostMilieu.equalsIgnoreCase("localhost")) {
			initDatabaseSource("Localhost_" + request.getContextPath().replace("/", ""));
		}

	}

	public ConnectionFactory(String dbName) throws Exception {
		HttpServletRequest request;
		request = ServletActionContext.getRequest();
		
		String hostMilieu = request.getSession().getServletContext().getInitParameter("hostMilieu");
		if (hostMilieu.equalsIgnoreCase("SERVER")) {
			initDatabaseSource(dbName);
		} else if ((hostMilieu == null) || (hostMilieu.equalsIgnoreCase("TEST"))) {
			initDatabaseSource("Test_" + dbName);
		} else if (hostMilieu.equalsIgnoreCase("COM")) {
			initDatabaseSource("Com_" + request.getContextPath().replace("/", ""));
		} else if (hostMilieu.equalsIgnoreCase("LOCALHOST")) {
			initDatabaseSource("Localhost_" + dbName);
		}
	}

//	public static ConnectionFactory getInstance() {
//		System.out.println("instance = "+instance);
//		if (instance == null) {
//			try {
//				instance = new ConnectionFactory();
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
//		}
//		return instance;
//	}
//
//	public static ConnectionFactory getInstance(String dbName) {
//		if (instance2 == null) {
//			try {
//				instance2 = new ConnectionFactory(dbName);
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
//		}
//		return instance2;
//	}

	public synchronized Connection getConnection() throws Exception {
		try {
			Class.forName(driver).newInstance();
			conn =  DriverManager.getConnection(url, user, userPwd);
			conn.setAutoCommit(false);
		} catch (ClassNotFoundException ex) {
			System.err.println("Cannot find the database driver classes.");
		}catch (Exception ex) {
			throw ex;
		}
		return conn;
	}

	public synchronized Connection getDBPoolConnection() throws Exception {
		try {
			Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
			ResourceBundle rb = ResourceBundle.getBundle("proxool");
			Properties info = new Properties();
			info.setProperty("proxool.minimum-connection-count", rb.getString("minimum-connection-count"));
			info.setProperty("proxool.maximum-connection-count", rb.getString("maximum-connection-count"));
			info.setProperty("proxool.maximum-connection-lifetime", rb.getString("maximum-connection-lifetime"));
			info.setProperty("proxool.maximum-active-time", rb.getString("maximum-active-time"));
			info.setProperty("proxool.house-keeping-test-sql", rb.getString("house-keeping-test-sql"));
			info.setProperty("proxool.trace", rb.getString("trace"));

			info.setProperty("user", user);
			info.setProperty("password", userPwd);
			String alias = rb.getString("alias");
			String connectionInfo = "proxool." + alias + ":" + driver + ":" + url;
			conn = DriverManager.getConnection(connectionInfo, info);
			conn.setAutoCommit(false);
		} catch (ClassNotFoundException ex) {
			System.err.println("Cannot find the database driver classes.");
		}catch (Exception ex) {
			ex.printStackTrace();
			System.err.println("Cannot connect to this database.");
			throw ex;
		}
		return conn;
	}

	private void initDatabaseSource(String dbName) throws SQLException {
		ResourceBundle rb = ResourceBundle.getBundle("database");
		db = rb.getString(dbName + "_database");
		driver = rb.getString(dbName + "_driver");
		url = rb.getString(dbName + "_url");
		user = rb.getString(dbName + "_user");
		userPwd = rb.getString(dbName + "_password");	
		
//		log.info("db = " + db);
//		log.info("driver = " + driver);
//		log.info("url = " + url);
//		log.info("user = " + user);
//		log.info("userPwd = " + userPwd);
	}
}
